<?php
	/**
	 * 商品管理控制器
	 */
	class GoodsAction extends CommonAction{

		//商品列表首页
		public function index(){
			import('ORG.Util.Page');
			$db = M('goods');
			$goodsCounts = $db->count();
			$page = new Page($goodsCounts, 4);
			$limit = $page->firstRow . ',' . $page->listRows;
			$goods = $db->limit($limit)->select();
			$this->goodsCounts = $goodsCounts;
			$this->goods = $goods;
			$this->page = $page->show();
			$this->display();
		}

		//添加商品列表页
		public function addGoods(){
			$db = M("category");
			$category = $db->select();
			$brands = $db->table("hd_brand")->field("id,name")->select();
			//分配商品分类到视图
			$this->category = recursion($category , 0 ,1);
			//分配商品品牌到视图
			$this->brands = $brands;
			$this->display();
		}

		//异步获取属性与规格
		public function getAttr(){
			$tid = $this->_post('tid','intval');
			$db = M("type_attr");
			$typeAttr =  $db->where('tid='.$tid)->select();
			if(is_array($typeAttr)){
				echo json_encode($typeAttr);	
			}else{//没有类型数据则返回状态 0
				echo json_encode(array('ermsg'=>'0'));
			}
		}

		//处理提交的新添加的商品信息
		public function runAddGoods(){
			// p($_POST);
			$data = array(
				'name' => $_POST['goodsname'],
				'mprice' => $_POST['mprice'],
				'sprice' => $_POST['sprice'],
				'unit' => $_POST['unit'],
				'inventory' => $_POST['inventory'],
				'click' => $_POST['clicksum'],
				'listpic' => $_POST['listpic'],
				'time' => time(),
				'number' => $_POST['gnumber'],
				'aid' => (int)session('uid'),
				'bid' => empty($_POST['bid'])? 0 : (int)$_POST['bid'],
				'cid' => (int)$_POST['cid'],
				'tid' => (int)$_POST['tid']
				);
			$intro = array(
				'max' => implode('|',$_POST['max']),
				'medium' => implode('|', $_POST['medium']),
				'mini' => implode('|', $_POST['mini']),
				);
			$intro['intro'] = $_POST['goodsintroduce'];
			$intro['service'] = $_POST['aftersale'];
			$attr = $_POST['attr'];
			$spec = $_POST['spec'];
			$goodsAttr = array();		
			//组合商品属性
			foreach($attr as $key => $val){
				$goodsAttr[] = array(
						'value' => $val,
						'aid' => (int)$key
					);
			}
			//组合商品规格
			foreach($spec as $key => $val){
				for($i = 0; $i < count($val['value']); $i++){
					$goodsAttr[] = array(
						'value' => $val['value'][$i],
						'price' => (float)$val['price'][$i],
						'aid' => $key
						);
				}
			}
			$data['intro'] = $intro;
			$data['goods_attr'] = $goodsAttr;
			//实例化关联模型
			$db = D('GoodsRelation');
			if($db->relation(true)->add($data)){
				$this->success('添加商品成功！',U('index'));
			}else{
				$this->error('添加商品失败！');
			}
		}

		//添加货品--具体进货来某件的商品
		public function addItem(){
			$data = array(
				'number' => $_POST['number'],
				'inventory' => $_POST['inventory'],
				'gid' => (int)$_POST['gid'],
				'attr' => implode('|', $_POST['attr'])
				);
			$db = M("goods_list");
			if($db->add($data)){
				$this->success('添加货品成功！' , U('itemList' , array('gid' => (int)$_POST['gid'])));
			}else{
				$this->error('添加货品失败！');
			}
		}

		//货品列表视图
		public function itemList(){
			//货品列表分页
			import('ORG.Util.Page');
			$gid = $this->_get('gid','intval');
			$db = M("goods_attr");
			$aid = $db->where(array('gid' => $gid))->field('aid')->group('aid')->select();
			foreach($aid as $key => $val){
				$aid[$key] = $val['aid'];
			}
			$where = array('id'=>array('IN' , $aid) , 'type' => 1);
			//获取该商品的规格
			$typeName = M("type_attr")->where($where)->field('id,name')->select();
			$specCounts = count($typeName);
			//将该商品所具有的规格提取出来
			foreach($typeName as $val){
				$where = array('aid' => $val['id'] , 'gid' => $gid);
				$spec[] = $db->field('id,value')->where($where)->select();
			}
			//货品列表分页控制
			$itemCounts = M('goods_list')->where('gid=' . $gid)->count();
			$page = new Page($itemCounts , 10);
			$limit = $page->firstRow .',' . $page->listRows; 
			$itemslist = M("goods_list")->where('gid=' . $gid)->limit($limit)->select();
			foreach($itemslist as $key => $val){
				$where = array('id' => array('IN' , explode('|', $val['attr'])));
				$itemslist[$key]['attr'] = $db->where($where)->field('value')->select();
			}
			$this->itemCounts = $itemCounts;
			$this->itemslist = $itemslist;
			$this->page = $page->show();
			//分配商品的规格参数到货品视图
			$this->spec = $spec;
			//分配商品不同的规格货品视图
			$this->typeName = $typeName;
			//用于调整页面表格布局
			$this->pageColspan = $specCounts + 4;
			$this->display();
		}
	}
?>